package com.example.mvvmtemplate.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.jetbrains.annotations.NotNull;

public abstract class BaseFragment extends Fragment {

    public View view;
    public Context mContext;
    public BaseActivity mActivity;

    public abstract int getLayoutResID();

    public void onCreateView(View view) {

    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            mActivity = (BaseActivity) context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutResID(), container, false);
        onCreateView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getContext();
    }

    public void startActivity(Class<?> cls) {
        mActivity.startActivity(cls);
    }

    /**
     * 重写startActivity
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        mActivity.startActivity(cls, bundle);
    }

    public void startActivityForResult(Class<?> cls, int requestCode) {
        mActivity.startActivityForResult(cls, requestCode);
    }

    public void startActivityForResult(Class<?> cls, int requestCode, Bundle bundle) {
        mActivity.startActivityForResult(cls, requestCode, bundle);
    }

    public void showToast(String msg) {
        mActivity.showToast(msg);
    }
}
